package com.eling.elcms.basedoc.webapp.controller;

import com.eling.elcms.basedoc.model.ComOrgRelation;
import com.eling.elcms.basedoc.model.CommunityData;
import com.eling.elcms.basedoc.service.IComOrgRelationManager;
import com.eling.elcms.basedoc.service.ICommunityDataManager;
import com.eling.elcms.community.service.IOrganizationManager;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class CommunityDataCotroller extends BaseFormController{

	@Autowired
	private ICommunityDataManager communityDataManager;
	@Autowired
	private IComOrgRelationManager comOrgRelationManager;
	@Autowired
	private IOrganizationManager  organizationManager;
	@RequestMapping("/api/communitydata/search*")
	@ResponseBody
	public List<CommunityData> search(@ModelAttribute SearchCondition sc) {
		return communityDataManager.search(sc); 
	}
	
	@RequestMapping("/api/communitydata/query")
    @ResponseBody
    public List<CommunityData> query(@ModelAttribute CommunityData cond){
    	return communityDataManager.query(cond); 
    }
	@RequestMapping("/api/communitydata/comorgrelation")
    @ResponseBody
    public List<CommunityData> query(@ModelAttribute CommunityData cond ,
    		@RequestParam(value="pkOrganization") Long pkOrganization){
		
		List<CommunityData> list = new ArrayList<CommunityData>();
		ComOrgRelation comOrgRelation = new ComOrgRelation();
		comOrgRelation.setCommunityData(cond);
		if(pkOrganization != 0L){
			comOrgRelation.setOrganization(organizationManager.get(pkOrganization));
		}
		List<ComOrgRelation> comOrgRelations= comOrgRelationManager.query(comOrgRelation);
		for(int i = 0;i<comOrgRelations.size();i++){
			list.add(comOrgRelations.get(i).getCommunityData());
		}
    	return list; 
    }
	@RequestMapping("/api/communitydata/save")
    @ResponseBody
    public CommunityData save(@ModelAttribute@ModelFilling CommunityData serviceCommunity,
    		@RequestParam(value="pkOrganization") Long pkOrganization) {
		if(serviceCommunity.getEnableTime()==null){
			serviceCommunity.setEnableTime(new Date());
		}
		CommunityData communityData = communityDataManager.save(serviceCommunity);
		ComOrgRelation comOrgRelation = new ComOrgRelation();
		comOrgRelation.setOrganization(organizationManager.get(pkOrganization));
		comOrgRelation.setCommunityData(communityData);
		if(comOrgRelationManager.query(comOrgRelation).size()==0){
			comOrgRelationManager.save(comOrgRelation);
		}
    	return communityData;
    }
    
    @RequestMapping("/api/communitydata/{pkCommunityData}/delete")
    @ResponseBody
    public Msg delete(@PathVariable Long pkCommunityData) {
    	communityDataManager.remove(pkCommunityData);
    	return new Msg("删除成功");
    }
    //启用
    @RequestMapping("api/communitydata/{pkCommunityData}/open")
	@ResponseBody
	public CommunityData open(@PathVariable Long pkCommunityData) {
		return communityDataManager.open(pkCommunityData);
	}
    //停用
    @RequestMapping("api/communitydata/{pkCommunityData}/close")
	@ResponseBody
	public CommunityData close(@PathVariable Long pkCommunityData) {
		return communityDataManager.close(pkCommunityData);
	}
}
